package com.yint.http.bodys;

import java.util.ArrayList;
import java.util.List;

import com.yint.http.values.PartValue;

public class HttpFormBody extends AbsBody
{

	private List< PartValue > params = new ArrayList< PartValue >();

	public HttpFormBody()
	{

	}

	public void addFormData( PartValue part )
	{
		if ( part != null )
		{
			params.add( part );
		}
	}

	@Override
	protected String getStrContent()
	{
		StringBuffer buffer = new StringBuffer();
		for ( int i = 0; i < params.size(); i++ )
		{
			buffer.append( params.get( i ).getKey() ).append( "=" );
			buffer.append( params.get( i ).getValue() );
			if ( i < ( params.size() - 1 ) )
			{
				buffer.append( "&" );
			}
		}
		return buffer.toString();
	}

	@Override
	protected byte[] getByteContent()
	{
		String content = getStrContent();
		if ( content != null )
		{
			return content.getBytes();
		}
		return null;
	}
}
